<template>
<div class="user">
    <!--搜索表单-->
    <div class="common-seach-wrap">
      <el-form
        size="small"
        :inline="true"
        :model="searchForm"
        class="demo-form-inline"
      >
        <el-form-item :label="$t('plus.order_number')">
          <el-input
            size="small"
            v-model="searchForm.orderNo"
            :placeholder="$t('plus.input_order_no')"
          ></el-input>
        </el-form-item>
        <el-form-item :label="$t('plus.delivery_code')">
          <el-input
            size="small"
            v-model="searchForm.codeNo"
            :placeholder="$t('plus.delivery_code')"
          ></el-input>
        </el-form-item>
        <el-form-item :label="$t('plus.delivery_status1')">
          <el-select
            size="small"
            v-model="searchForm.deliveryStatus"
            :placeholder="$t('plus.please_select')"
          >
            <el-option :label="$t('plus.all')" value=""></el-option>
            <el-option :label="$t('plus.status_wait_delivery')" value="0"></el-option>
            <el-option :label="$t('plus.status_delivered')" value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('plus.category')">
          <el-select
            size="small"
            v-model="searchForm.categoryId"
            :placeholder="$t('plus.please_select')"
          >
            <el-option :label="$t('plus.all')" value=""></el-option>
            <el-option
              v-for="(item, index) in categoryList"
              :key="index"
              :label="item.name"
              :value="item.categoryId"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('plus.order_time1')">
          <div class="block">
            <span class="demonstration"></span>
            <el-date-picker
              size="small"
              v-model="searchForm.createTime"
              type="daterange"
              value-format="YYYY-MM-DD"
              :range-separator="$t('plus.to')"
              :start-placeholder="$t('plus.start_date')"
              :end-placeholder="$t('plus.end_date')"
            ></el-date-picker>
          </div>
        </el-form-item>
        <el-form-item>
          <el-button size="small" type="primary" icon="Search" @click="onSubmit"
            >{{$t('plus.search')}}</el-button
          >
        </el-form-item>
        <!--<el-form-item>
          <el-button size="small" type="success" @click="onExport" v-auth="'/order/order/operate/export'">导出</el-button>
        </el-form-item>-->
      </el-form>
    </div>
    <!--内容-->
    <div class="product-content">
      <div class="table-wrap">
        <el-tabs v-model="activeName" @tab-click="handleClick">
          <el-tab-pane :label="$t('plus.all_orders')" name="all"></el-tab-pane>
          <el-tab-pane
            :label="$t('plus.order_pending_delivery_count') + orderCount + ')'"
            name="wait"
          ></el-tab-pane>
        </el-tabs>
        <el-table
          size="small"
          :data="tableData.data"
          border
          style="width: 100%"
          v-loading="loading"
        >
          <el-table-column prop="userId" :label="$t('plus.user_id_lower')"></el-table-column>
          <el-table-column prop="nickName" :label="$t('plus.user_nickname')"></el-table-column>
          <el-table-column prop="orderNo" :label="$t('plus.order_number')"></el-table-column>
          <el-table-column
            prop="card.cardTitle"
            :label="$t('plus.related_coupon')"
          ></el-table-column>
          <el-table-column
            prop="cardCode.codeNo"
            :label="$t('plus.delivery_code')"
          ></el-table-column>
          <el-table-column
            prop="productName"
            :label="$t('plus.delivery_product')"
          ></el-table-column>
          <el-table-column prop="name" :label="$t('plus.delivery_person')"></el-table-column>
          <el-table-column prop="mobile" :label="$t('plus.delivery_phone')"></el-table-column>
          <el-table-column prop="deliveryStatus" :label="$t('plus.delivery_status1')">
            <template #default="scope">
              <span class="gray" v-if="scope.row.deliveryStatus == 0"
                >{{$t('plus.status_wait_delivery')}}</span
              >
              <span class="green" v-if="scope.row.deliveryStatus == 1"
                >{{$t('plus.status_delivered')}}</span
              >
            </template>
          </el-table-column>
          <el-table-column prop="createTime" :label="$t('plus.delivery_period')"></el-table-column>
          <el-table-column fixed="right" :label="$t('plus.action')" width="90">
            <template #default="scope">
              <el-button
                @click="addClick(scope.row)"
                type="text"
                size="small"
                v-auth="'/order/order/detail'"
                >{{$t('plus.details')}} 
</el-button>
              <el-button
                v-if="scope.row.deliveryStatus == 0"
                @click="addClick(scope.row)"
                type="text"
                size="small"
                v-auth="'/order/order/delivery'"
              > {{$t('plus.delivery')}} 

</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!--分页-->
      <div class="pagination">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          background
          :current-page="curPage"
          :page-size="pageSize"
          layout="total, prev, pager, next, jumper"
          :total="totalDataNumber"
        ></el-pagination>
      </div>
    </div>
    <el-dialog :title="$t('plus.batch_delivery_note')" v-model="dialogBatchDelivery" width="30%">
      <div>{{$t('plus.batch_step_1')}}</div>
      <div> {{$t('plus.batch_step_2')}} 

</div>
      <div> {{$t('plus.batch_step_3')}}
<el-link
          type="primary"
          :underline="false"
          @click="gotoExpress()"
          >{{$t('plus.view')}}</el-link
        >
      </div>
      <div style="padding-top: 20px; padding-bottom: 20px; text-align: center">
        <el-upload
          class="avatar-uploader"
          ref="upload"
          action="string"
          accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
          :before-upload="onBeforeUploadImage"
          :http-request="UploadImage"
          :show-file-list="false"
        >
          <el-button size="small" icon="Upload" type="primary"
            >{{$t('plus.upload_delivery_excel')}}</el-button
          >
        </el-upload>
      </div>
    </el-dialog>
    <!--处理-->
    <Cancel
      v-if="open_edit"
      :open_edit="open_edit"
      :order_no="order_no"
      @closeDialog="closeDialogFunc($event, 'edit')"
    >
    </Cancel>
  </div>
</template>

<script>
import CardApi from "@/api/card.js";
import qs from "qs";
import Cancel from "./dialog/cancel.vue";
export default {
  components: {
    Cancel
  },
  data() {
    return {
      /*切换菜单*/
      activeName: "all",
      /*是否加载完成*/
      loading: true,
      /*列表数据*/
      tableData: [],
      /*一页多少条*/
      pageSize: 20,
      /*一共多少条数据*/
      totalDataNumber: 0,
      /*当前是第几页*/
      curPage: 1,
      /*横向表单数据模型*/
      searchForm: {
        orderNo: "",
        codeNo: "",
        categoryId: "",
        deliveryStatus: "",
        createTime: ""
      },
      /*配送方式*/
      exStyle: [],
      /*门店列表*/
      categoryList: [],
      /*时间*/
      createTime: "",
      /*统计*/
      orderCount: 0,
      dialogBatchDelivery: false,
      /*是否打开编辑弹窗*/
      open_edit: false,
      /*当前编辑的对象*/
      order_no: 0
    };
  },
  created() {
    /*获取列表*/
    this.getData();
  },
  methods: {
    /*选择第几页*/
    handleCurrentChange(val) {
      let self = this;
      self.curPage = val;
      self.getData();
    },
    /*每页多少条*/
    handleSizeChange(val) {
      this.curPage = 1;
      this.pageSize = val;
      this.getData();
    },
    /*切换菜单*/
    handleClick(tab, event) {
      let self = this;
      self.curPage = 1;
      self.getData();
    },
    /*获取列表*/
    getData() {
      let self = this;
      let Params = this.searchForm;
      Params.dataType = self.activeName;
      Params.pageIndex = self.curPage;
      Params.pageSize = self.pageSize;
      Params.startTime = Params.createTime != "" ? Params.createTime[0] : null;
      Params.endTime = Params.createTime != "" ? Params.createTime[1] : null;
      self.loading = true;
      CardApi.orderlist(Params, true).then(res => {
        self.tableData.data = res.data.list.records;
        self.totalDataNumber = res.data.list.total;
        self.orderCount = res.data.orderCount;
        self.categoryList = res.data.categoryList;
        self.loading = false;
      }).catch(error => {});
    },
    /*打开取消*/
    cancelClick(item) {
      this.order_no = item.order_no;
      this.open_edit = true;
    },
    /*关闭弹窗*/
    closeDialogFunc(e, f) {
      if (f == "edit") {
        this.open_edit = e.openDialog;
        if (e.type == "success") {
          this.getData();
        }
      }
    },
    /*打开添加*/
    addClick(row) {
      let self = this;
      let params = row.orderId;
      self.$router.push({
        path: "/plus/card/order/detail",
        query: {
          orderId: params
        }
      });
    },
    /*搜索查询*/
    onSubmit() {
      this.curPage = 1;
      this.tableData = [];
      this.getData();
    },
    onExport: function () {
      let baseUrl = window.location.protocol + "//" + window.location.host;
      window.location.href = baseUrl + "/index.php/shop/order.operate/export?" + qs.stringify(this.searchForm);
    },
    // 显示批量发货说明
    showBatchDelivery() {
      this.dialogBatchDelivery = true;
    },
    /*跳转物流公司*/
    gotoExpress() {
      let self = this;
      self.$router.push("/setting/express/index");
    },
    /*选择图片之前*/
    onBeforeUploadImage(file) {
      var fileType = file.name.substring(file.name.lastIndexOf(".") + 1);
      const isEXCEL = fileType === "xlsx";
      const isLt10M = file.size / 1024 / 1024 < 10;
      if (!isEXCEL) {
        ElMessage.error($t("plus.upload_excel_only"));
      }
      if (!isLt10M) {
        ElMessage.error($t("plus.upload_max_10mb"));
      }
      return isEXCEL && isLt10M;
    },
    /*上传图片*/
    UploadImage(param) {
      let self = this;
      const loading = ElLoading.service({
        lock: true,
        text: $t("plus.processing"),
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)"
      });
      const formData = new FormData();
      formData.append("iFile", param.file);
      OrderApi.batchDelivery(formData).then(response => {
        loading.close();
        self.dialogBatchDelivery = false;
        ElMessage({
          message: response.msg,
          type: "warning"
        });
      }).catch(response => {
        loading.close();
        ElMessage({
          message: $t("plus.process_failed"),
          type: "warning"
        });
        param.onError();
      });
    }
  }
};
</script>

<style lang="scss">
.product-info {
  padding: 10px 0;
  border-top: 1px solid #eeeeee;
}

.order-code .state-text {
  padding: 2px 4px;
  border-radius: 4px;
  background: #808080;
  color: #ffffff;
}

.order-code .state-text-red {
  background: red;
}

.table-wrap .product-info:first-of-type {
  border-top: none;
}

.table-wrap .el-table__body tbody .el-table__row:nth-child(odd) {
  background: #f5f7fa;
}
</style>

